var VM = new Vue({
    el:'#app',//vm接管了app区域的管理
    data:{//model数据
        listData:[],
        insertData:{
            'title':'',
            'description':'',
            'content':'',
        },
        updateData:{},
        name:'Hello Word'
    },
    onload(){
        //this.init();
        this.refreshList();
    },
    created(){
        this.refreshList();
    },
    methods:{
        
        init:function () {
            this.refreshList();
        },
        //刷新数据列表
        refreshList:function(){
            window.gitee.getCodelist().then((res)=>{
                if(!res.message){
                    this.listData=res;
                }
                
            });
        },
        //关闭并打印
        toPrint(text){
            utools.copyText(text)
            window.utools.hideMainWindow()
            utools.simulateKeyboardTap('v', 'ctrl')
        },
        //点击选择
        clickItem(item){
            console.log(item);
            let content=item.files[item.description].content;
            console.log(content);
            this.toPrint(content);
        },
        //设置access_token
        setAccessToken(){
            let self=this;
            let giteeToken=db.get("giteeToken");
        
            layer.prompt({
                title: '输入AccessToken，并确认',
                value:giteeToken,
                formType: 3
            }, function(text, index){
                window.setDB(text);
                window.gitee.access_token=text;
                self.refreshList();
                layer.close(index);
              });
        },
        //打开新增弹窗
        insertItemAlert(){
            layer.open({
                type: 2,
                title: '新增代码片段',
                closeBtn:1,
                shadeClose: !false,
                //shade: !true,
                maxmin: true, //开启最大化最小化按钮
                area: ['400px', '500px'],
                content:'pages/insert.html'
              });
        },
       //打开修改弹窗
        updateItemAlert(item){
        console.log('insertItemAlert');
        this.updateData=item;
        layer.open({
            type: 2,
            title: '修改代码片段',
            closeBtn:1,
            shadeClose: !false,
            //shade: !true,
            maxmin: true, //开启最大化最小化按钮
            area: ['400px', '500px'],
            content:'pages/update.html'
          });
        },
        //删除
        deleteItem(item){
            let self=this;

            layer.open({
                title:'是否确认删除',
                content: '确认删除-'+item.description+'?'
                ,btn: ['确认', '取消']
                ,yes: function(index, layero){
                  //按钮【按钮一】的回调
                  window.gitee.deleteCode(item.id).then((res)=>{
                    layer.msg('删除成功!');
                    self.refreshList();
                });
                }
                ,btn2: function(index, layero){
                  //按钮【按钮二】的回调
                  
                  //return false 开启该代码可禁止点击该按钮关闭
                }
                ,cancel: function(){ 
                  //右上角关闭回调
                  
                  //return false 开启该代码可禁止点击该按钮关闭
                }
              });
            return false;  
            window.gitee.deleteCode(item.id).then((res)=>{
                console.log("res",res);
                this.refreshList();
            });
        },
        clearDB(){
            window.db.clear();
        }
        //
    }
});